package com.vivi.feverdiagnose.db;

import java.util.ArrayList;
import java.util.List;

import com.vivi.feverdiagnose.DataGejala;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

public class DataSource {
	private SQLiteDatabase mSQL;
	private DBHandler mDBH;
	
	public DataSource (Context context){
		mDBH = new DBHandler(context);
	}

	public void open() throws SQLiteException {
		mSQL = mDBH.getWritableDatabase();
	}
	
	public void close() {
		mDBH.close();
	}
	
	//insert into tb_penyakit
	public void insertPenyakit(String idPenyakit, String nama, String saran){

		ContentValues values = new ContentValues();
		
		values.put(DBHandler.COLUMN_ID_PENYAKIT, idPenyakit);
		values.put(DBHandler.COLUMN_NAMA_PENYAKIT, nama);
		values.put(DBHandler.COLUMN_SARAN, saran);
		
		mSQL.insert(DBHandler.TABLE_PENYAKIT, null, values);
		
	}
	
	//insert into tb_gejala
	public void insertGejala(String idGejala, String nama){

		ContentValues values = new ContentValues();
		
		values.put(DBHandler.COLUMN_ID_GEJALA, idGejala);
		values.put(DBHandler.COLUMN_NAMA_GEJALA, nama);		
		
		mSQL.insert(DBHandler.TABLE_GEJALA, null, values);
		
	}
	
	//insert into tb_detail
	//public void insertDetail(String id_detail, String id_penyakit, String id_gejala){

		//ContentValues values = new ContentValues();
		
		//values.put(DBHandler.COLUMN_ID_DETAIL, id_detail);
	
		
		//mSQL.insert(DBHandler.TABLE_DETAIL, null, values);
		
	//}
	
	// Select All Data Kuis
	public List<DataGejala> SelectAllDataGejala() {
		// TODO Auto-generated method stub
		List<DataGejala> listGejala = new ArrayList<DataGejala>();
		
		 try {
			 mSQL = mDBH.getReadableDatabase(); // Read Data
			 
			 String strSQL = "SELECT * FROM " + DBHandler.TABLE_GEJALA;
			 Cursor cursor = mSQL.rawQuery(strSQL, null);
			 
			 	if(cursor != null) 	{
					if (cursor.moveToFirst()) {						
						do {				
							DataGejala gjl = new DataGejala();
							gjl.setIdGjl(cursor.getInt(0));
							gjl.setNamaGjl(cursor.getString(1));

							listGejala.add(gjl);
						} while (cursor.moveToNext());						

					}
			 	}
			 	cursor.close();
			 	mDBH.close();
				return listGejala;
				
		 } catch (Exception e) {
		    return null;
		 }

	}
}
